package hotel.meituan.oversea.biz.service.oversea;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.constant.enums.MtHotelEnums;
import hotel.base.oversea.utils.Result;
import hotel.meituan.oversea.api.enums.oversea.MtHotelOverseaEnums;
import hotel.meituan.oversea.api.vo.overseaRequestVO.MtHotelChangeCallbackVO;
import hotel.meituan.oversea.api.vo.overseaRequestVO.MtHotelDetailVO;
import hotel.meituan.oversea.api.vo.overseaRequestVO.MtHotelGoodsPriceVO;
import hotel.meituan.oversea.api.vo.overseaRequestVO.MtHotelGoodsRpVO;
import hotel.meituan.oversea.api.vo.overseaRequestVO.MtHotelIdsVO;
import hotel.meituan.oversea.api.vo.overseaRequestVO.MtHotelOrderBookingVO;
import hotel.meituan.oversea.api.vo.overseaRequestVO.MtHotelOrderCancelVO;
import hotel.meituan.oversea.api.vo.overseaRequestVO.MtHotelOrderCheckVO;
import hotel.meituan.oversea.api.vo.overseaRequestVO.OrderQueryParamVO;
import hotel.meituan.oversea.api.vo.overseaResponceVO.MtHotelBookingOrderResult;
import hotel.meituan.oversea.api.vo.overseaResponceVO.MtHotelDetail;
import hotel.meituan.oversea.api.vo.overseaResponceVO.MtHotelGoodsPrice;
import hotel.meituan.oversea.api.vo.overseaResponceVO.MtHotelGoodsRp;
import hotel.meituan.oversea.api.vo.overseaResponceVO.MtHotelIds;
import hotel.meituan.oversea.api.vo.overseaResponceVO.MtHotelRealRoomVO;
import hotel.meituan.oversea.api.vo.overseaResponceVO.MtHotelOrderCheckResult;
import hotel.meituan.oversea.api.vo.overseaResponceVO.OrderCancelResult;
import hotel.meituan.oversea.api.vo.overseaResponceVO.OrderQuery;
import hotel.meituan.oversea.biz.task.MeituanSaveApiLogTask;
import hotel.meituan.oversea.biz.utils.MtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class MtHotelOverseaServiceImpl implements MtHotelOverseaService {
    @Autowired
    MeituanSaveApiLogTask meituanSaveApiLogTask;

    /**
     * @Description 查询
     * @Author Chen_l
     * @Date 2021/7/23 18:27
     */
    @Override
    public Result<Object> mtHotelGetIds(int maxId, int pageSize) {
        MtHotelIdsVO reqVO = MtHotelIdsVO.builder().maxId((long) maxId).pageSize(pageSize).build();
        String reqStr = JSON.toJSONString(reqVO);
        String respStr = MtUtils.createParamsPost(reqStr, MtHotelOverseaEnums.MTapimethod.GETHOTELIDS.getMethod(), meituanSaveApiLogTask);
        JSONObject respJson = JSONObject.parseObject(respStr);
        MtHotelIds mtHotelIds = null;
        if (Objects.equals(String.valueOf(respJson.get("code")), "0")) {
            JSONObject resultJson = respJson.getJSONObject("result");//获取响应的数据
            if (resultJson != null) {
                mtHotelIds = JSON.parseObject(JSON.toJSONString(resultJson), MtHotelIds.class);
                if (mtHotelIds != null) {
                    List<Long> hotelIds = mtHotelIds.getHotelIds();
                    if (hotelIds != null && hotelIds.size() > 0) {
                        return new Result<>(mtHotelIds);
                    } else {
                        log.info("///////////////MTAPIERROR///////////////查询酒店ID列表接口数据异常  hotelIDS is null ：respStr-{}，reqStr-{}", respStr, reqStr);
                        return new Result<>(mtHotelIds, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                    }
                } else {
                    log.info("///////////////MTAPIERROR///////////////查询酒店ID列表接口数据异常  parse error ：respStr-{}，reqStr-{}", respStr, reqStr);
                    return new Result<>(mtHotelIds, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                }
            } else {
                log.error("///////////////MTAPIERROR///////////////查询酒店ID列表接口数据异常  result is null ：respStr-{}，reqStr-{}", respStr, reqStr);
                return new Result<>(mtHotelIds, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
            }
        } else {
            log.error("///////////////MTAPIERROR///////////////查询酒店ID列表接口数据异常  code != 0 ：respStr-{}，reqStr-{}", respStr, reqStr);
            return new Result<>(mtHotelIds, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
        }
    }

    //http://localhost:10030/mtOverSeaHotel/getMtHotelDetail
    @Override
    public Result<Object> mtHotelGetDetails(MtHotelDetailVO mtHotelDetailVo) {
        Integer stratery = mtHotelDetailVo.getStrategy();
        if (stratery == null) {
            stratery = MtHotelOverseaEnums.MtHotelDetail.ALL.getCode();
            mtHotelDetailVo.setStrategy(stratery);
        }
        String reqStr = JSON.toJSONString(mtHotelDetailVo);
        //请求获取酒店详情数据接口
        String respStr = MtUtils.createParamsPost(reqStr, MtHotelEnums.apiMethodEnum.METHOD_HOTEL_DETAIL.getMethod(), meituanSaveApiLogTask);
        JSONObject respJson = JSONObject.parseObject(respStr);
        List<MtHotelDetail> mtHotelDetails = null;
        if (Objects.equals(respJson.getString("code"), "0")) {
            JSONObject resultJSON = respJson.getJSONObject("result");
            if (resultJSON != null) {
                try {
                    mtHotelDetails = JSONObject.parseArray(JSON.toJSONString(resultJSON), MtHotelDetail.class);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    log.info("//////////////////////// 美团酒店详情异常 getMtHotelDetail parse error ：respStr-{}，reqStr-{}", respStr, reqStr);
                    return new Result<>(mtHotelDetails, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                }
                if (mtHotelDetails != null && mtHotelDetails.size() > 0) {
                    //接口成功返回酒店详情数据
                    return new Result<>(mtHotelDetails, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                } else {
                    log.info("//////////////////////// 美团酒店详情异常 该酒店查无信息 ：respStr-{}，reqStr-{}", respStr, reqStr);
                    return new Result<>(mtHotelDetails, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                }
            } else {
                log.info("//////////////////////// 美团酒店详情异常 getMtHotelDetail result is null ：respStr-{}，reqStr-{}", respStr, reqStr);
                return new Result<>(mtHotelDetails, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
            }
        } else {
            log.info("/////////////////查询美团酒店详情异常///////////////// code != 0  mtHotelGetDetails : respStr-{} reqStr-{}", respStr, reqStr);
            return new Result<>(mtHotelDetails, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
        }
    }

    @Override
    public Result<Object> mtHotelGetRealRoom(List<Long> hotelIds) {
        //构建请求参数,发起请求
        String reqStr = "{\"hotelIds\":" + hotelIds.toString() + "}";
        System.out.println(hotelIds.toString());
        System.out.println(reqStr);
        String respStr = MtUtils.createParamsPost(reqStr, MtHotelOverseaEnums.MTapimethod.GETREALROOM.getMethod(), meituanSaveApiLogTask);
        JSONObject respJson = JSONObject.parseObject(respStr);
        MtHotelRealRoomVO mtHotelRealRoomVO = null;
        if (Objects.equals(respJson.getString("code"), "0")) {
            String resultStr = respJson.getString("result");
            if (!StringUtils.isBlank(resultStr)) {
                try {
                    mtHotelRealRoomVO = JSONObject.parseObject(respJson.toJSONString(), MtHotelRealRoomVO.class);
                    if (mtHotelRealRoomVO != null) {
                        //接口成功返回
                        return new Result<>(mtHotelRealRoomVO);
                    } else {
                        log.info("//////////////////////// 美团酒店查询物理房型异常 mtHotelGetRealRoom VO is null : respStr-{} reqStr-{}", respStr, reqStr);
                        return new Result<>(mtHotelRealRoomVO, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    log.info("//////////////////////// 美团酒店查询物理房型异常 mtHotelGetRealRoom parse error : respStr-{} reqStr-{}", respStr, reqStr);
                    return new Result<>(mtHotelRealRoomVO, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                }
            } else {
                log.info("//////////////////////// 美团酒店查询物理房型 mtHotelGetRealRoom result null : respStr-{} reqStr-{}", respStr, reqStr);
                return new Result<>(mtHotelRealRoomVO, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
            }
        } else {
            log.info("///////////////查询美团酒店物理房型异常   mtHotelGetRealRoom code!=0 : respStr-{} reqStr-{}//////////////////", respStr, reqStr);
            return new Result<>(mtHotelRealRoomVO, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
        }


    }

    @Override
    public Result<Object> mtHotelGetGoodsRp(MtHotelGoodsRpVO mtHotelGoodsRpVO) {
        String reqStr = JSON.toJSONString(mtHotelGoodsRpVO);
        //发起请求
        String respStr = MtUtils.createParamsPost(reqStr, MtHotelOverseaEnums.MTapimethod.GETGOODS.getMethod(), meituanSaveApiLogTask);
        JSONObject respJson = JSONObject.parseObject(respStr);
        MtHotelGoodsRp mtHotelGoodsRp = null;
        if (Objects.equals(respJson.getString("code"), "0")) {
            String resultStr = respJson.getString("result");
            if (StringUtils.isNotBlank(resultStr)) {
                try {
                    mtHotelGoodsRp = JSONObject.parseObject(respStr, MtHotelGoodsRp.class);
                    if (mtHotelGoodsRp != null) {
                        //接口成功返回
                        return new Result<>(mtHotelGoodsRp);
                    } else {
                        log.info("//////////////////////// 美团酒查询酒店产品列表异常 mtHotelGetGoodsRp VO is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                        return new Result<>(mtHotelGoodsRp, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    log.info("//////////////////////// 美团酒店查询酒店产品列表异常 gtHotelGetGoodsRp parse error  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                    return new Result<>(mtHotelGoodsRp, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                }
            } else {
                log.info("//////////////////////// 美团酒查询酒店产品列表异常 resultStr  is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                return new Result<>(mtHotelGoodsRp, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
            }
        } else {
            log.info("//////////////////////// 美团酒店查询酒店产品列表异常 gtHotelGetGoodsRp  code ！= 0  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
            return new Result<>(mtHotelGoodsRp, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
        }

    }

    @Override
    public Result<Object> mtHotelGetGoodsPrice(MtHotelGoodsPriceVO mtHotelGoodsPriceVO) {
        String reqStr = JSON.toJSONString(mtHotelGoodsPriceVO);
        String respStr = MtUtils.createParamsPost(reqStr, MtHotelOverseaEnums.MTapimethod.GETGOODSPRICE.getMethod(), meituanSaveApiLogTask);
        JSONObject respJson = JSONObject.parseObject(respStr);
        MtHotelGoodsPrice mtHotelGoodsPrice = null;
        if (respJson != null) {
            if (Objects.equals(respJson.getString("code"), "0")) {
                try {
                    mtHotelGoodsPrice = JSONObject.parseObject(respJson.toJSONString(), MtHotelGoodsPrice.class);
                    if (mtHotelGoodsPrice != null) {
                        //接口成功返回
                        return new Result<>(mtHotelGoodsPrice);
//                        return new Result<>(respStr);
                    } else {
                        log.info("//////////////////////// 美团查询产品价格日历异常 mtHotelGoodsPrice is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                        return new Result<>(mtHotelGoodsPrice, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    log.info("//////////////////////// 美团查询产品价格日历异常 mtHotelGetGoodsPrice parse error  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                    return new Result<>(mtHotelGoodsPrice, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                }
            } else {
                log.info("//////////////////////// 美团查询产品价格日历异常 mtHotelGetGoodsPrice code ！= 0  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                return new Result<>(mtHotelGoodsPrice, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
            }
        } else {
            log.info("//////////////////////// 美团查询产品价格日历异常 mtHotelGetGoodsPrice respJson is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
            return new Result<>(mtHotelGoodsPrice, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
        }
    }

    @Override
    public Result<Object> mtHotelOrderCheck(MtHotelOrderCheckVO mtHotelGoodsPriceVO) {
        String reqStr = JSON.toJSONString(mtHotelGoodsPriceVO);
        String respStr = MtUtils.createParamsPost(reqStr, MtHotelOverseaEnums.MTapimethod.ORDERCHECK.getMethod(), meituanSaveApiLogTask);
        JSONObject respJson = JSONObject.parseObject(respStr);
        MtHotelOrderCheckResult orderCheckResult = null;
        if (respJson != null) {
            if (Objects.equals(respJson.getString("code"), "0")) {
                String resultStr = respJson.getString("result");
                try {
                    orderCheckResult = JSONObject.parseObject(resultStr, MtHotelOrderCheckResult.class);
                    if (orderCheckResult != null) {
                        //接口成功返回
                        return new Result<>(orderCheckResult);
                    } else {
                        log.info("//////////////////////// 下单前校验异常 mtHotelOrderCheck is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                        return new Result<>(orderCheckResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    log.info("//////////////////////// 下单前校验异常  mtHotelOrderCheck parse error  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                    return new Result<>(orderCheckResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                }
            } else {
                log.info("//////////////////////// 下单前校验异常 mtHotelOrderCheck code ！= 0  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                return new Result<>(orderCheckResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
            }
        } else {
            log.info("//////////////////////// 下单前校验异常 mtHotelOrderCheck respJson is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
            return new Result<>(orderCheckResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
        }
    }

    @Override
    public Result<Object> mtHotelOrderBooking(MtHotelOrderBookingVO mtHotelGoodsPriceVO) {
        String reqStr = JSON.toJSONString(mtHotelGoodsPriceVO);
        String respStr = MtUtils.createParamsPost(reqStr, MtHotelOverseaEnums.MTapimethod.ORDERBOOKING.getMethod(), meituanSaveApiLogTask);
        JSONObject respJson = JSONObject.parseObject(respStr);
        MtHotelBookingOrderResult mtHotelBookingOrderResult = null;
        if (respJson != null) {
            if (Objects.equals(respJson.getString("code"), "0")) {
                String resultStr = respJson.getString("result");
                try {
                    mtHotelBookingOrderResult = JSONObject.parseObject(resultStr, MtHotelBookingOrderResult.class);
                    if (mtHotelBookingOrderResult != null) {
                        //接口成功返回
                        return new Result<>(mtHotelBookingOrderResult);
                    } else {
                        log.info("//////////////////////// 预约下单异常 mtHotelOrderBooking is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                        return new Result<>(mtHotelBookingOrderResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    log.info("//////////////////////// 预约下单异常  mtHotelOrderBooking parse error  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                    return new Result<>(mtHotelBookingOrderResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                }
            } else {
                log.info("//////////////////////// 预约下单异常 mtHotelOrderBooking code ！= 0  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                return new Result<>(mtHotelBookingOrderResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
            }
        } else {
            log.info("//////////////////////// 预约下单异常 mtHotelOrderBooking respJson is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
            return new Result<>(mtHotelBookingOrderResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
        }
    }

    @Override
    public Result<Object> mtHotelOrderCancel(MtHotelOrderCancelVO mtHotelGoodsPriceVO) {
        String reqStr = JSON.toJSONString(mtHotelGoodsPriceVO);
        String respStr = MtUtils.createParamsPost(reqStr, MtHotelOverseaEnums.MTapimethod.ORDERCANCEL.getMethod(), meituanSaveApiLogTask);
        JSONObject respJson = JSONObject.parseObject(respStr);
        OrderCancelResult orderCancelResult = null;
        if (respJson != null) {
            if (Objects.equals(respJson.getString("code"), "0")) {
                String resultStr = respJson.getString("result");
                try {
                    orderCancelResult = JSONObject.parseObject(resultStr, OrderCancelResult.class);
                    if (orderCancelResult != null) {
                        //接口成功返回
                        return new Result<>(orderCancelResult);
                    } else {
                        log.info("//////////////////////// 取消订单异常 mtHotelOrderCancel is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                        return new Result<>(orderCancelResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    log.info("//////////////////////// 取消订单异常  mtHotelOrderCancel parse error  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                    return new Result<>(orderCancelResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                }
            } else {
                log.info("//////////////////////// 取消订单异常 mtHotelOrderCancel code ！= 0  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                return new Result<>(orderCancelResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
            }
        } else {
            log.info("//////////////////////// 取消订单异常 mtHotelOrderCancel respJson is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
            return new Result<>(orderCancelResult, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
        }
    }

    @Override
    public Result<Object> mtHotelOrderQuery(OrderQueryParamVO orderQueryParamVO) {
        String reqStr = JSON.toJSONString(orderQueryParamVO);
        String respStr = MtUtils.createParamsPost(reqStr, MtHotelOverseaEnums.MTapimethod.ORDERQUERY.getMethod(), meituanSaveApiLogTask);
        JSONObject respJson = JSONObject.parseObject(respStr);
        OrderQuery orderQuery = null;
        if (respJson != null) {
            if (Objects.equals(respJson.getString("code"), "0")) {
                try {
                    orderQuery = JSONObject.parseObject(respJson.toJSONString(), OrderQuery.class);
                    if (orderQuery != null) {
                        //接口成功返回
                        return new Result<>(orderQuery);
                    } else {
                        log.info("//////////////////////// 查询订单异常 mtHotelOrderCancel is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                        return new Result<>(orderQuery, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    log.info("//////////////////////// 查询订单异常  mtHotelOrderCancel parse error  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                    return new Result<>(orderQuery, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
                }
            } else {
                log.info("//////////////////////// 查询订单异常 mtHotelOrderCancel code != 0  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
                return new Result<>(orderQuery, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
            }
        } else {
            log.info("//////////////////////// 查询订单异常 mtHotelOrderCancel respJson is null  respStr-{} reqStr-{}//////////////////", respStr, reqStr);
            return new Result<>(orderQuery, respJson.getString("message"), Integer.parseInt(respJson.getString("code")));
        }
    }


    @Override
    public Result<Object> mtHotelChangeCallback(MtHotelChangeCallbackVO mtHotelChangeCallbackVO) {
        String reqStr = JSON.toJSONString(mtHotelChangeCallbackVO);
        String respStr = MtUtils.createParamsPost(reqStr, MtHotelOverseaEnums.MTapimethod.CHANGECALLBACK.getMethod(), meituanSaveApiLogTask);
        JSONObject respJson = JSONObject.parseObject(respStr);
        if (respJson != null && Objects.equals(respJson.getString("code"), "0") && Objects.equals(respJson.getString("message"), "成功")) {
            //请求成功
            return new Result<>("订单状态变更通知成功");
        } else {
            log.info("//////////////////////// 订单状态变更异常 respStr-: {}，reqStr-: {}", respStr, reqStr);
            return new Result<>("订单状态变更通知失败");
        }
    }
}
